/**
 * @brief 
 * @author zhangpeihao
 */

#pragma once

#include <zsystem/core/common/event_types.h>
#include <zsystem/core/common/logdump.h>
#include "st_connection.h"

#define DISCONNECTED_TIMEOUT 5 // Remove user after disconnected 5 seconds
class user {
public:
  user(const Z_I32 connectionId, const Z_I32 uid)
  : _connectionId(connectionId)
  , _uid(uid)
  , _disconnectedAt(0)
  , _isReconnected(Z_FALSE){
    Z_ASSERT(_uid > 0);
  }
  virtual ~user(){};
  const Z_I32 uid() const {
    return _uid;
  }
  const Z_I32 connectionId() {
    return _connectionId;
  }
  void reconnected(const Z_I32 connectionId){
    _connectionId = connectionId;
    _disconnectedAt = 0;
    _isReconnected = Z_TRUE;
  }
  void disconnected(){
    _connectionId = -1;
    _disconnectedAt = (Z_I32)time(Z_NULL);
  }
  const Z_I32 disconectedAt() const {
    return _disconnectedAt;
  }
  const Z_BOOL isDisconnectedTimeout() const {
    return (_disconnectedAt > 0) && (_disconnectedAt + DISCONNECTED_TIMEOUT < (Z_I32)time(Z_NULL));
  }
  const Z_BOOL isReconnected() const {
    return _isReconnected;
  }
protected:
  Z_I32 _connectionId;
  Z_I32 _uid;
  Z_I32 _disconnectedAt;
  Z_BOOL _isReconnected;
};
